home *** CD-ROM | disk | FTP | other *** search
/ Palm Utilities / Palm_Utilities_CD-ROM_2001_2001.iso / files / internet misc / GetTLE 1.0 / GetTLE.exe / Src / PSatDB.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-15  |  4.3 KB  |  166 lines

  1. /*
  2.     GetTLE - writedb.c - Handling of PocketSat database 
  3.     Copyright ⌐2000 Andreas Schneider
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. */
  19.  
  20. #include <PalmOS.h>
  21. #include "alerts.h"
  22. #include "Progress.h"
  23. #include "debug.h"
  24. #include "PSatDB.h"
  25. #include "GetTLERsc.h" // should use progress.c instead
  26.  
  27. // structures and defines imposed by other programs
  28. typedef struct {
  29.   char note; // actually a null terminated string
  30. } psatDBRecordType;
  31.  
  32. typedef psatDBRecordType * psatDBRecordPtr;
  33.  
  34. #define psatCreatorID 'pSat'
  35. #define psatDBName "GetTLE satellites"
  36. #define psatDBType 'iTLE'
  37.  
  38. // globals
  39. DmOpenRef psatDB=0;
  40.  
  41. extern void OpenPSatDB(void)
  42. {
  43.   Err err;
  44.   psatDB=DmOpenDatabaseByTypeCreator(psatDBType,psatCreatorID,dmModeReadWrite);
  45.   if (psatDB)
  46.   {
  47.     // database exists and could be opened - no problems
  48.     // LogMessage("Database found\n");
  49.   }
  50.   else
  51.   {
  52.     // maybe database just doesn't exist yet - create it
  53.     LogMessage("Database not found");
  54.     err=DmCreateDatabase(0,psatDBName,psatCreatorID,psatDBType,false);
  55.     if (err)
  56.     {
  57.       LogMessage("failed to create database\n");
  58.     }
  59.     else
  60.     {
  61.       psatDB=DmOpenDatabaseByTypeCreator(psatDBType,psatCreatorID,dmModeReadWrite);
  62.       if (psatDB)
  63.       {
  64.         // LogMessage("Database now found\n");
  65.       }
  66.       else
  67.       {
  68.         LogMessage("Database still not found.\n");
  69.       }
  70.     }
  71.   }
  72.   return;
  73. }
  74.  
  75. extern void PSatDBAppendLine(Char *line)
  76. {
  77.   UInt16 index=10000; // end of database
  78.   MemHandle recordHandle;
  79.   psatDBRecordPtr recordPtr;
  80.   Err result;
  81.   UInt32 length;
  82.   
  83.   // LogMessage(line);
  84.      if (psatDB)
  85.      {
  86.        // allocate memory
  87.     length=(UInt32)StrLen(line);
  88.        recordHandle=(MemHandle)DmNewHandle(psatDB,(UInt32)(length+1)); // +1 - the trailin 0
  89.        if (recordHandle!=NULL)
  90.        {
  91.          // copy string 
  92.            recordPtr=MemHandleLock(recordHandle);
  93.            DmStrCopy(recordPtr,0,line);
  94.            MemPtrUnlock(recordPtr);
  95.            // append to database
  96.            result=DmAttachRecord(psatDB,&index,recordHandle,0); 
  97.            if (result) 
  98.            {
  99.              // release memory
  100.              MemHandleFree(recordHandle);
  101.            }
  102.        }
  103.        else
  104.        {
  105.         LogMessage("Can't get new handle");
  106.     }
  107.      }
  108.      else
  109.      {
  110.        LogMessage("Can't find memo database");
  111.      }
  112.      return;
  113. }
  114.  
  115. extern void ClearPSatDB(void)
  116. {
  117.   UInt16 num_records;
  118.   Char message[50]="";
  119.   LocalID dbID;
  120.   Err error;
  121.  
  122.   if (psatDB)
  123.   {
  124.     // how many records are there in the database
  125.     num_records=DmNumRecordsInCategory(psatDB,dmAllCategories);
  126.     // 3 lines are one TLE set - therefore:
  127.     StrPrintF(message,"Delete %i TLE sets?",num_records / 3);
  128.     if (MyConfirmFunc(message)==true)
  129.     {
  130.       // first close the database - can't delete it while it's open
  131.       ClosePSatDB();  
  132.       // next get it's LocalId from the database name
  133.       dbID=DmFindDatabase(0,psatDBName);
  134.       if (dbID==0)
  135.       {
  136.         // DmFindDatabase returns 0 if database not found
  137.         MyErrorFunc("Couldn't find database",psatDBName);
  138.         return;
  139.       }
  140.       // try deleteing the database
  141.       error=DmDeleteDatabase(0,dbID);
  142.       if (error!=0)
  143.       {
  144.         // DmDeleteDatabase returns 0 on success
  145.         MyErrorFunc("Couldn't delete Database",psatDBName);
  146.         LogMessage("Delete DB error #%i\n",error);
  147.         return;
  148.       }
  149.       MyStatusFunc("Database erased.");
  150.       // next create a new, empty database
  151.       OpenPSatDB();
  152.     }
  153.   }
  154.   return;
  155. }
  156.  
  157. // the usual cleanup
  158. extern void ClosePSatDB(void)
  159. {
  160.   if (psatDB)
  161.   {
  162.     DmCloseDatabase(psatDB);
  163.   }
  164.   psatDB=NULL;
  165.   return;
  166. }